﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12

import "../components/"
import "../"

Window {

    id: root

    title: qsTr("打印报告")
    width: 1530*dp
    height: 960*dp
    minimumWidth: 1530*dp
    minimumHeight: 960*dp
    maximumHeight: 960*dp
    maximumWidth: 1530*dp

    color: "#F7F7FA"

    property string localPrinterName: "导出为本地文件(docx)"
    property real dp: config.dp

    Config{
        id:config
    }

    ReportHistoryList{
        id: layout_history
        x: 50*dp; y: 50*dp
        height: parent.height - 2*y - 50*dp
        width: Math.max(parent.width*0.34, 600*dp)
    }

    onVisibleChanged: {
        if(visible){
            layout_history.initCheckedState()
        }
    }

    function refreshPrinterReview(){

        // word版本，已弃用 ========================
//        CPrinter.refreshPreview(dp)
//        image_preview.source = "image://printPreviewImage/" + Math.random()
//        let model = CPrinter.getPrinterList()
//        model.splice(0, 0, localPrinterName)
//        combo_printer.model = model

        // excel版本
        CPrinter.refreshReportInfo()
        layout_review.refreshInfo()
    }

//    Rectangle{
//        id: layout_review
//        color: "#FFF"
//        border.color: "#1677FF"
//        x: layout_history.x + layout_history.width + 30*dp
//        y: 50*dp
//        height: parent.height - 2*y
//        width: 600*dp
//        CustomImage{
//            id: image_preview
//            x: 2*dp; y: 2*dp
//            anchors.fill: parent
////            source: "qrc:/picture/printTemplate.png"
//            source: "image://printPreviewImage"
//        }
//        Text{
//            y: parent.height+4*dp
//            text: "* 图片仅供参考，最终结果请以实物为准"
//            font.pixelSize: config.fontsize - 2
//            opacity: 0.8
//        }
//    }

    // 报告信息
    ReportInfo{
        id: layout_review
        border.color: "#BBB"
        x: layout_history.x + layout_history.width + 16*dp
        y: 50*dp
        height: parent.height - 2*y - 50*dp
        width: 800*dp
    }

    // 都excel了就不加打印了吧…………
//    Item{
//        x: layout_review.x + layout_review.width + 30*dp
//        width: parent.width - x - 30*dp
//        y: 50*dp
//        height: parent.height - 100*dp
//        Column{
//            spacing: 20*dp
//            Item{
//                height: 40*dp; width: 40*dp
//                Text{
//                    text: "打印设置"
//                    font.bold: true
//                    font.pixelSize: config.fontsize + 4*dp
//                }
//            }
//            CustomComboBox{
//                id: combo_printer
//                title: "打印机"
//                editWidth: 220*dp
//                model: ["OneNote (DeskTop)", "HP-7200"]
//            }CustomComboBox{
//                title: "纸张大小"
//                editWidth: 220*dp
//                model: ["A4", "B5"]
//            }
//        }
//        Item{
//            height: 40*dp; width: parent.width
//            y: 250*dp
//            PushButton{
//                title: "取消"
//                defaultMode2: true
//                x: parent.width/2-130*dp
//                onReleased: {
//                    root.close()
//                }
//            }

//            PushButton{
//                title: "打印"
//                defaultMode1: true
//                x: parent.width/2+10*dp
//                onReleased: {

//                    if(layout_history.checkedIdx < 0){
//                        toast.show("请先选个实验去", 2)
//                        return
//                    }

//                    if(combo_printer.getText() === localPrinterName){
//                        let i = CReport.exportReport_fileDialog()
//                        console.log("printResult", i)
//                        if(i === 0){
//                            toast.show("打印成功", 0)
//                            return
//                        }else if(i === -1){
//                            box_error_common.box_text = "打印失败，文件模板缺失";
//                        }else if(i === -2){
//                            box_error_common.box_text = "打印失败，所选路径无法访问"
//                        }else if(i === -3){
//                            box_error_common.box_text = "打印失败，压缩文件缺失"
//                        }else if(i === -4){
//                            box_error_common.box_text = "打印失败, 所选路径无法访问"
//                        }else if(i >= 0){
//                            return
//                        }
//                        box_error_common.show()
//                    }else{
//                        CPrinter.print(combo_printer.getText())
//                    }
//                }
//            }
//        }
//    }

    Row{
        height: 40*dp; width: implicitWidth
        y: parent.height - height - 40*dp
        anchors.right: layout_review.right
        spacing: 2*dp
        CustomButton{
            title: "打印报告"
            defaultMode2: true
            onClicked: {
                box_isPrinting.show()
                timer_print.start()
            }
        }

        Timer{
            id: timer_print
            interval: 50
            onTriggered: {
                let res = CPrinter.printExcel("./report/autoExport_" + layout_review.getExpStartTime() + ".xlsx")
                box_isPrinting.close()
                if(res === -1){
                    box_printError.box_text = "打印文件缺失"
                    box_printError.show()
                }else if(res === -2){
                    box_printError.box_text = "连接Excel失败"
                    box_printError.show()
                }
            }

        }

        CustomPopupBoxWarning{
            id: box_isPrinting
            box_text: "正在连接Excel，请等待"
            showRejectedButton: false
        }CustomPopupBoxError{
            id: box_printError
            box_text: "打印文件缺失"
        }

        CustomButton{
            title: "导出报告"
            defaultMode1: true
            onClicked: {
                CReport.exportReport_fileDialog(layout_review.expInfo, layout_review.logInfo)
            }
        }

    }
    CustomPopupBoxError{
        id: box_error_common
    }
    CustomToast{
        id: toast
        x: (parent.width-width) * 0.5
        y: 200*dp
    }

}
